package com.ruoyi.common.utils;

import java.util.*;

/**
 * @author liuzhisheng
 * @descripition
 * @date 2020/11/12
 */
@SuppressWarnings("ALL")
public class GiftDTO {


    /**
     * 索引
     */
    private Integer index;


    /**
     * 奖品名称
     */
    private String name;

    /**
     * 奖品编号
     */
    private String no;


    /**
     * 中奖概率
     */
    private Double probability;

    public GiftDTO() { }

    public GiftDTO(Integer index, String name, String no, Double probability) {
        this.index = index;
        this.name = name;
        this.no = no;
        this.probability = probability;
    }

    public Integer getIndex() {
        return index;
    }

    public void setIndex(Integer index) {
        this.index = index;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public Double getProbability() {
        return probability;
    }

    public void setProbability(Double probability) {
        this.probability = probability;
    }


    public static int lottery(List<Double> orignalRates) {

        if (orignalRates == null || orignalRates.isEmpty()) {
            return -1;
        }

        int size = orignalRates.size();

        // 计算总概率，这样可以保证不一定总概率是1
        double sumRate = 0d;
        for (double rate : orignalRates) {
            sumRate += rate;
        }

        // 计算每个物品在总概率的基础下的概率情况
        List<Double> sortOrignalRates = new ArrayList<>(size);
        Double tempSumRate = 0d;
        for (double rate : orignalRates) {
            tempSumRate += rate;
            sortOrignalRates.add(tempSumRate / sumRate);
        }

        // 根据区块值来获取抽取到的物品索引
        double nextDouble = Math.random();
        sortOrignalRates.add(nextDouble);
        Collections.sort(sortOrignalRates);

        return sortOrignalRates.indexOf(nextDouble);
    }
    public static Integer luckdraw(List<GiftDTO> gifts) {
        Integer no = 0;
        // 存储概率
        List<Double> orignalRates = new ArrayList<>(gifts.size());
        for (GiftDTO gift : gifts) {
            double probability = gift.getProbability();
            if (probability < 0) {
                probability = 0;
            }
            orignalRates.add(probability);
        }
        // 统计
        Map<Integer, Integer> count = new HashMap<>();
        // 测试次数
        double num = 1;
        for (int i = 0; i < num; i++) {
            int orignalIndex = lottery(orignalRates);
            Integer value = count.get(orignalIndex);
            count.put(orignalIndex, value == null ? 1 : value + 1);
        }
        for (Map.Entry<Integer, Integer> entry : count.entrySet()) {
//            System.out.println(gifts.get(entry.getKey()).getIndex() + ", 命中次数=" + entry.getValue() + ", 实际概率="
//////                    + entry.getValue() / num);
           no =  gifts.get(entry.getKey()).getIndex();
        }
        return  no;
    }

    public static List<Integer> luckdrawMany(List<GiftDTO> gifts,int num) {
        List<Integer> indexList = new ArrayList<>();
        // 存储概率
        List<Double> orignalRates = new ArrayList<>(gifts.size());
        for (GiftDTO gift : gifts) {
            double probability = gift.getProbability();
            if (probability < 0) {
                probability = 0;
            }
            orignalRates.add(probability);
        }
        // 循环次数
        for (int i = 0; i < num; i++) {
            int orignalIndex = lottery(orignalRates);
            indexList.add(orignalIndex);
        }
        return  indexList;
    }
}
